<template>
  <!--列表-->
  <router-exact-view name="bussiness">
    <div style="width: 100%" class="template-list-page-container">
      <my-table
        v-loading="loading"
        :columns="columns"
        :data-source="data"
        :pagination="pagination"
        :btn-options="btnOptions"
        title="业务指导列表"
        @page-change="handlePageChange"
      >
        <div slot="name" slot-scope="scope">
          <pdf-enter
            :src="scope.row.url"
            :words="scope.row.name"
          />
        </div>
        <div slot="table-btn">
          <el-button type="primary" @click="addFile">新增业务指导</el-button>
        </div>
      </my-table>
    </div>
    <el-dialog title="新增业务指导" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="业务指导名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="文件类型" :label-width="formLabelWidth">
          <el-select v-model="form.fileType" placeholder="请选择文件类型">
            <el-option label="视频" :value="1" />
            <el-option label="pdf文件" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.fileType === 1" label="视频链接" :label-width="formLabelWidth">
          <el-input v-model="form.url" autocomplete="off" />
        </el-form-item>
        <el-form-item v-if="form.fileType === 2" label="上传文件" :label-width="formLabelWidth">
          <my-upload v-model="form.url" list-type="text" img-type="pdf,PDF" :limit="1" />
          <div v-if="!form.url || form.url.length === 0" style="color: #bbb;">请上传pdf格式的文件</div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitFile">确 定</el-button>
      </div>
    </el-dialog>
  </router-exact-view>
</template>

<script>
import { getFileList, saveFile, delFile, stickFile } from '@/api/operate'
import MyTable from '@/components/my-table/index'
import RouterExactView from '@/components/routerExactView'
import MyUpload from '@/components/my-upload/index'
import PdfEnter from '@/components/pdf-enter'
import { toHttps } from '@/utils/index'
import { bussinessColumns } from '../columns'

export default {
  components: { RouterExactView, MyTable, MyUpload, PdfEnter },
  data() {
    const that = this
    return {
      /* 弹窗相关 */
      dialogFormVisible: false,
      form: {
        name: '',
        file: [],
        type: 1
      },
      formLabelWidth: '120px',
      /* 列表相关 */
      columns: bussinessColumns,
      data: [],
      searchForm: {
      },
      loading: false,
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0
      },
      btnOptions: [
        {
          name: '删除',
          handler: function(row) {
            that.deleteFile(row)
          }
        },
        {
          name: '置顶',
          v_if: row => row.stick === 0,
          handler: function(row) {
            that.setTopFile(row, '置顶')
          }
        },
        {
          name: '取消置顶',
          v_if: row => row.stick === 1,
          handler: function(row) {
            that.setTopFile(row, '取消置顶')
          }
        }
      ]
    }
  },
  mounted() {
    if (this.$route.name === 'bussiness') {
      this.handlePageChange()
    }
  },
  methods: {
    async handlePageChange(current, size) {
      if (current) {
        this.pagination.current = current
      }
      if (size) {
        this.pagination.pageSize = size
      }
      this.loading = true
      try {
        const res = await getFileList({
          pageNum: this.pagination.current,
          pageSize: this.pagination.pageSize,
          type: 3
        })
        if (res && res.list) {
          this.data = res.list.map(v => ({ ...v, url: toHttps(v.url) }))
          this.pagination = {
            current: res.pageNum,
            pageSize: res.pageSize,
            total: res.totalElements
          }
        }
        this.loading = false
      } catch (err) {
        this.loading = false
      }
    },
    resetSearchForm() {
      this.searchForm = {}
      this.handlePageChange(1)
    },
    addFile() {
      this.dialogFormVisible = true
      this.form = {}
    },
    async submitFile() {
      if (this.form.name && (this.form.fileType === 1 && this.form.url) || (this.form.fileType === 2 && this.form.url && this.form.url.length > 0)) {
        const params = JSON.parse(JSON.stringify(this.form))
        delete params.fileType
        if (this.form.fileType === 2) {
          params.url = this.form.url[0].url
        }
        try {
          const res = await saveFile({
            ...params,
            type: 3
          })
          if (res) {
            this.$message({
              type: 'success',
              message: '新增业务指导成功！'
            })
            this.dialogFormVisible = false
            this.handlePageChange(1)
          }
        } catch (err) {
          console.log(err)
        }
      } else {
        this.$message({
          type: 'warning',
          message: '请将表单信息填写完整'
        })
      }
    },
    async deleteFile(row) {
      const that = this
      this.$confirm('确定要删除该业务指导？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(async() => {
        try {
          const res = await delFile({ id: row.id })
          if (res) {
            that.$message({
              message: `删除业务指导成功！`,
              type: 'success'
            })
            that.handlePageChange(that.pagination.current)
          }
        } catch (err) {
          console.log('接口出错')
        }
      }).catch(() => {
        console.log('取消')
      })
    },
    async setTopFile(row, word) {
      const that = this
      this.$confirm(`确定要${word}该业务指导？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(async() => {
        try {
          const res = await stickFile({ id: row.id })
          if (res) {
            that.$message({
              message: `{word}业务指导成功！`,
              type: 'success'
            })
            that.handlePageChange(1)
          }
        } catch (err) {
          console.log('接口出错')
        }
      }).catch(() => {
        console.log('取消')
      })
    }
  }
}
</script>
<style scoped lang="less">
  @import "../../common.css";
  /* 查询条件容器的样式 */
  .search-area {
    padding: 16px 16px 0;
    margin-bottom: 8px;
    background: #fff;
  }
  a {
    color: #409EFF;
  }
</style>
